package chen.web.controller.forum;

import java.util.List;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import chen.web.widget.vote.Vote;
import chen.web.widget.vote.VoteManager;
import chen.web.widget.vote.VoteOption;

public class VoteController {

	private @Autowired VoteManager vm;
	
	@RequestMapping(value="/vote/{id}", method=RequestMethod.PUT)
	public @ResponseBody JSONResponse vote(@PathVariable int id, @RequestParam int[] optionsOrder) throws JSONException{
		Vote vote = vm.get(id);
		List<VoteOption> options = vote.getOptions();
		VoteOption[] result = new VoteOption[optionsOrder.length];
		
		for(int i=0; i<optionsOrder.length; i++){
			result[i] = options.get(optionsOrder[i]);
		}
		
		vm.vote(result);
		
		return JSONResponse.successed();
	}
	
}
